package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping()
    @ApiOperation("新增地址")
    public Result addAddressBook(@RequestBody AddressBook addressBook) {
        log.info("<新增地址> : {}" , addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result listAll(){
        log.info("查询当前登录用户的所有地址信息");
        //?
        List<AddressBook> addressList = addressBookService.listByUserId(BaseContext.getCurrentId());
        return Result.success(addressList);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result selectDefault(){
        log.info("<查询默认地址> ");
        AddressBook addressBook = addressBookService.selectDefault(BaseContext.getCurrentId());
        return Result.success(addressBook);
    }


    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping("")
    @ApiOperation("根据id修改地址")
    public Result updateById(@RequestBody AddressBook addressBook){

        log.info("<根据id修改地址> : {}" , addressBook);
        addressBookService.updateById(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    //这个传过来就是参数
    @DeleteMapping()
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id){
        log.info("根据id删除地址 : {}" , id);
        addressBookService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result selectById(Long id){
        log.info("<根据id查询地址> : {}" , id);
        //?
        AddressBook addressBook = addressBookService.selectById(id);
        return Result.success(addressBook);
    }

//注意这个前端传过来的还是对象
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefalut( @RequestBody AddressBook addressBook){
        log.info("设置默认地址 : {}" , addressBook);

        addressBookService.setDefalut(addressBook);
        return Result.success();

    }








}
